package cn.dujc;

import java.awt.*;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        move2trash(args);
    }

    private static void move2trash(String... args) {
        if (!Desktop.getDesktop().isSupported(Desktop.Action.MOVE_TO_TRASH)) {
            System.out.println("TrashJ is not support move to trash, please use /bin/rm to delete file");
        } else if (args != null && args.length > 0) {
            List<File> files = Arrays.stream(args)
                    .filter(arg -> !arg.startsWith("-") && new File(arg).exists())
                    .map(File::new)
                    .collect(Collectors.toList());
            files.forEach(file -> {
                final String fileName = file.getName();
                if (fileName.startsWith(".")) {
                    File _file = new File(file.getParentFile(), "_" + fileName);
                    if (file.renameTo(_file)) {
                        System.out.println("rename success, hidden file["
                                + fileName + "] rename to " + _file.getName()
                                + " , then move to trash:  " + _file.getAbsolutePath());
                        Desktop.getDesktop().moveToTrash(_file);
                    } else {
                        System.out.println("rename failure, move to trash:  " + file.getAbsolutePath());
                        Desktop.getDesktop().moveToTrash(file);
                    }
                } else {
                    System.out.println("move to trash:  " + file.getAbsolutePath());
                    Desktop.getDesktop().moveToTrash(file);
                }
            });
        }
    }
}
